﻿// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

namespace Microsoft.Samples.BabelCam.Phone.PivotContent
{
    using System;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    using System.Windows.Navigation;
    using Microsoft.Phone.Shell;
    using Microsoft.Phone.Tasks;
    using Microsoft.Samples.BabelCam.Phone.ViewModel;

    public partial class TranslatedWordsTablePage : UserControl
    {
        public TranslatedWordsTablePage()
        {
            this.InitializeComponent();

            this.Loaded += this.OnTranslatedWordsTablePageLoaded;
        }

        public event EventHandler<NavigationEventArgs> Navigate
        {
            add { this.ViewModel.Navigate += value; }
            remove { this.ViewModel.Navigate -= value; }
        }

        public TranslatedWordsTablePageViewModel ViewModel
        {
            get { return this.DataContext as TranslatedWordsTablePageViewModel; }
            set { this.DataContext = value; }
        }

        private static void SetApplicationState(string key, object value)
        {
            if (PhoneApplicationService.Current.State.ContainsKey(key))
            {
                PhoneApplicationService.Current.State.Remove(key);
            }

            PhoneApplicationService.Current.State.Add(key, value);
        }

        private void OnTranslatedWordsTablePageLoaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (this.ViewModel != null)
            {
                this.ViewModel.LoadTable();
            }
        }

        private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.ViewModel != null)
            {
                var selector = sender as Selector;
                if ((selector == null) || (selector.SelectedIndex == -1))
                {
                    return;
                }

                var task = new WebBrowserTask();
                task.Uri = new Uri(this.ViewModel.Table[selector.SelectedIndex].ImageName);
                task.Show();
            }
        }
    }
}